﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using BusinessFacade;
using EntityManage;
public partial class Modules_Personal_PerfEvaluateOrgPersonalDetail : System.Web.UI.Page
{
    IReportHandler rptHandler = new ReportManage();
    IPerf_Personal_ApprovalHandler approvalHandler = new Perf_Personal_ApprovalManage();
    public ISys_UserHandler userHandler = new Sys_UserManage();
    DataBase db = new DataBase();
    IPerf_Personal_EvaluateDtlHandler evaluateDtlHandler = new Perf_Personal_EvaluateDtlManage();
    IPerf_Personal_EvaluateHandler evaluateHandler = new Perf_Personal_EvaluateManage();
    IPerf_Personal_TargetHandler targetHanler = new Perf_Personal_TargetManage();
    IPerf_Personal_TargetTypeHandler targettypeHandler = new Perf_Personal_TargetTypeManage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["UserId"] = Request["UserId"];
            ViewState["NowYear"] = DateTime.Now.Year;
            ViewState["Month"] = DateTime.Now.Month;
            List<SqlParameter[]> parms = null;
            string clause = " 1=1 ";

            ProcessSeasonParms(ref clause, ref parms);
            dataBind(clause, parms);
        }
    }
    /// <summary>
    /// 绑定季度绩效成绩
    /// </summary>
    private void dataBind(string whereClause, List<SqlParameter[]> parms)
    {
        DataTable dtNew = new DataTable();
        DataTable dtTargetTypeC = new DataTable();
        DataTable dtTargetC = new DataTable();
        SqlParameter[] dtTargetTypeParmsC = null;
        SqlParameter[] dtTargetParmsC = null;
        dtTargetTypeParmsC = parms[0];
        dtTargetParmsC = parms[1];
        dtNew.Columns.Add("员工名");
        dtNew.Columns.Add("总得分");
        //添加指标类型列
        dtTargetTypeC = rptHandler.GetPerTargetTypeTotalGrade(" Target_User_ID in(" + ViewState["UserId"].ToString() + ") " , dtTargetTypeParmsC);
        if (dtTargetTypeC != null && dtTargetTypeC.Rows.Count > 0)
        {
            for (int i = 0; i < dtTargetTypeC.Rows.Count; i++)
            {
                //添加指标类型列
                dtNew.Columns.Add(dtTargetTypeC.Rows[i]["TypeName"].ToString());
            }
        }
        //添加指标列
        dtTargetC = rptHandler.GetPerTargetScore(whereClause +" and pa.Target_User_ID in(" + ViewState["UserId"].ToString() + ")", dtTargetParmsC);
        DataView myDataView = new DataView(dtTargetC);
        string[] strComuns ={ "TargetName" };
        DataTable dt = myDataView.ToTable(true, strComuns);

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //添加指标类型列
                dtNew.Columns.Add(dt.Rows[i]["TargetName"].ToString());
            }
        }
        DataTable dtTarget = new DataTable();
        //循环获取所选员工填入数据
        for (int i = 0; i < ViewState["UserId"].ToString().Split(new char[] { ',' }).Length; i++)
        {
            DataRow row = dtNew.NewRow();
            row["员工名"] = ViewState["UserId"].ToString().Split(new char[] { ',' })[i].ToString();
            
            //获取该员工存在的指标的得分
            dtTarget = rptHandler.GetPerTargetScore(GetTimeWhere(ViewState["UserId"].ToString().Split(new char[] { ',' })[i].ToString()), null);
            if (dtTarget != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dtTarget.Rows.Count; j++)
                {
                    row[dtTarget.Rows[j]["TargetName"].ToString()] = dtTarget.Compute("Sum(TotalScore)", "TargetName='" + dtTarget.Rows[j]["TargetName"] + "'");//统计所有此指标名的分数
                    //获取相应指标类型添加分数
                    row[GetTargetTypeName(int.Parse(dtTarget.Rows[j]["TargetID"].ToString()))] = dtTarget.Compute("Sum(TotalScore)", "TargetType='" + dtTarget.Rows[j]["TargetType"] + "'");
                }
            }
            row["总得分"] = dtTarget.Compute("Sum(TotalScore)","true");//计算dtTarget里的所有指标分数和
            dtNew.Rows.Add(row);
        }

        this.gvPersonalList.DataSource = dtNew;
        gvPersonalList.AutoGenerateColumns = true;
        this.gvPersonalList.DataBind();
    }
    public string GetTargetTypeName(int targetId)
    {
        Perf_Personal_TargetQuery query = targetHanler.GetPerf_Personal_TargetQuery(targetId);
        Perf_Personal_TargetTypeQuery typeQuery = targettypeHandler.GetPerf_Personal_TargetTypeQuery(query.TargetType_ID);
        return typeQuery.Name;
    }
    public string GetTimeWhere(string userId)
    {
        string whereTime = string.Empty;
        //获取时间段
        if (int.Parse(ViewState["Month"].ToString()) >= 1 && int.Parse(ViewState["Month"].ToString()) <= 3)
        {
            whereTime = " begin_time between '" + ViewState["NowYear"] + "/01/01" + "' and '" + ViewState["NowYear"] + "/03/31" + "' ";
            
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 4 && int.Parse(ViewState["Month"].ToString()) <= 6)
        {
            whereTime = " begin_time between '" + ViewState["NowYear"] + "/04/01" + "' and '" + ViewState["NowYear"] + "/06/30" + "' ";
            
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 6 && int.Parse(ViewState["Month"].ToString()) <= 9)
        {
            whereTime = " begin_time between '" + ViewState["NowYear"] + "/07/01" + "' and '" + ViewState["NowYear"] + "/09/30" + "' ";
            
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 10 && int.Parse(ViewState["Month"].ToString()) <= 12)
        {
            whereTime = " begin_time between '" + ViewState["NowYear"] + "/10/01" + "' and '" + ViewState["NowYear"] + "/12/31" + "' ";
            
        }
        if (userId != string.Empty)
        {
            whereTime += " and pa.Target_User_ID=" + userId;
        }
        return whereTime;
    }
    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {

    }
    protected void ApprovalButton_Click(object sender, EventArgs e)
    {
        
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("PerfEvaluateOrgPersonalReport.aspx");
    }
    protected void gvPersonalList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "GetByName")//点击人员名称跳转到所有季度绩效
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), string.Format("window.location.href='PerfEvaluateOrgPersonalAllSeasonReport.aspx?UserId={0}'", e.CommandArgument.ToString()), true);
        }
        if (e.CommandName == "GetByScore")//点击分数跳转到该成绩的指标类型得分、指标得分
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), string.Format("window.location.href='PerfEvaluateOrgPersonalTargetTypeandTargetScore.aspx?UserId={0}&Month=" + ddlSeason.SelectedValue.ToString() + "'", e.CommandArgument.ToString()), true);
        }
    }
    protected void gvPersonalList_Sorting(object sender, GridViewSortEventArgs e)
    {

    }
    protected void gvPersonalList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex > -1)
            {
                // 员工名
                Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(int.Parse(e.Row.Cells[0].Text));
                if (userQuery != null)
                    e.Row.Cells[0].Text = userQuery.Name;

                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.Cells[i].Text.ToString() == string.Empty || e.Row.Cells[i].Text.ToString().ToLower() == "null" || e.Row.Cells[i].Text.ToString() == "")
                    {
                        e.Row.Cells[i].Text = "--";
                    }
                }
            }
            
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
    }
    /// <summary>
    /// 季度参数获取
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessSeasonParms(ref string whereClause, ref List<SqlParameter[]> parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();
        List<SqlParameter> lstParm2 = new List<SqlParameter>();
        List<SqlParameter> lstParm3 = new List<SqlParameter>();
        List<SqlParameter> lstParm4 = new List<SqlParameter>();
        parms = new List<SqlParameter[]>();
        //获取季度
        if (int.Parse(ViewState["Month"].ToString()) >= 1 && int.Parse(ViewState["Month"].ToString()) <= 3)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
            lstParm2.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm2.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
            lstParm3.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm3.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
            lstParm4.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm4.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 4 && int.Parse(ViewState["Month"].ToString()) <= 6)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
            lstParm2.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm2.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
            lstParm3.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm3.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
            lstParm4.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm4.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 6 && int.Parse(ViewState["Month"].ToString()) <= 9)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
            lstParm2.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm2.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
            lstParm3.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm3.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
            lstParm4.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm4.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
        }
        else if (int.Parse(ViewState["Month"].ToString()) >= 10 && int.Parse(ViewState["Month"].ToString()) <= 12)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
            lstParm2.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm2.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
            lstParm3.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm3.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
            lstParm4.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm4.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
        }
        parms.Add(lstParm.ToArray());
        parms.Add(lstParm2.ToArray());
        parms.Add(lstParm3.ToArray());
        parms.Add(lstParm4.ToArray());
    }
}
